//
//  WBOriginalStatusView.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/3.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit

class WBOriginalStatusView: UIView {

    var bottonCons : Constraint?
    var statusViewModel:WBStatusViewModel?
        {
        didSet
        {
            //给控件添加数据
            //头像
            iconView.sd_setImage(with: URL(string:statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: #imageLiteral(resourceName: "avatar_default"))
            //昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            //会员图标
            memberIconView.image = statusViewModel?.memberImage
            //会员认证图标
            avatarView.image = statusViewModel?.avatarImage
            //TODO:时间和来源暂时不处理
            sourceLabel.text = statusViewModel?.sourceString
            //createAtLabel.text = statusViewModel?.status?.created_at?.description
            createAtLabel.text = statusViewModel?.createAtString
            //内容
            contentLabel.text = statusViewModel?.status?.text
            self.bottonCons?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls,pic_urls.count > 0{
                //有图片
                pictureView.pic_urls = pic_urls
                //更新约束
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                   self.bottonCons = make.bottom.equalTo(pictureView).offset(WBStatusCellMargin).constraint
                })
                
            }else{
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                   self.bottonCons = make.bottom.equalTo(contentLabel).offset(WBStatusCellMargin).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUI()
    {
        self.backgroundColor = UIColor.white
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(createAtLabel)
        addSubview(sourceLabel)
        addSubview(avatarView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        iconView.snp_makeConstraints { (make)->Void in
            make.left.top.equalTo(self).offset(WBStatusCellMargin)
            make.width.height.equalTo(35)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(WBStatusCellMargin)
        }
        
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(WBStatusCellMargin)
        }
        
        //createAtLabel.text = "刚刚"
        createAtLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        //sourceLabel.text = "来自 微博客户端"
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(createAtLabel.snp_right).offset(WBStatusCellMargin)
            make.top.equalTo(createAtLabel)
        }
        
        avatarView.snp_makeConstraints { (make) in
            make.centerY.equalTo(iconView.snp_bottom).offset(-2)
            make.centerX.equalTo(iconView.snp_right).offset(-2)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(WBStatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make)->Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(WBStatusCellMargin)
            make.left.equalTo(contentLabel)
            
        }
        // 将原创微博的底部设置到内容的底部
        self.snp_makeConstraints { (make) -> Void in
           self.bottonCons = make.bottom.equalTo(pictureView).offset(WBStatusCellMargin).constraint
        }


    }
    ///头像
    lazy var iconView : UIImageView = {
        let imgView = UIImageView(image: #imageLiteral(resourceName: "avatar_default"))
        imgView.layer.cornerRadius = 17.5
        imgView.layer.masksToBounds = true
        return imgView
    }()
    ///昵称
    lazy var nameLabel : UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 14)
        label.text = "仓央嘉措"
        return label
    }()
    /// 会员图标
    private lazy var memberIconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "common_icon_membership"))
    /// 创建时间
    private lazy var createAtLabel: UILabel = UILabel(textColor: UIColor.orange, fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: 12)
    /// 认证的图标
    private lazy var avatarView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_vip"))
    /// 内容的label
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 15)
        // 设置多行模式
        label.numberOfLines = 0
        // 告诉系统，最大的布局宽度是多少
        label.preferredMaxLayoutWidth = WBScreenW - 2 * WBStatusCellMargin
        return label
    }()
    ///配图
    lazy var pictureView : WBStatusPictureView = {
        let view = WBStatusPictureView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
